#!/usr/bin/perl -ws
$trace = (defined($t) && ($t eq "1"));
if (!defined($is_cygwin) || ($is_cygwin eq "")) {
	$uname = `uname -a`;
	if ($trace) {
		print "uname=$uname\n";
	}
	if ($uname =~ /linux/i) {
		$is_cygwin = 0;
	} else {
		$is_cygwin = 1;
	}
	if ($trace) {
		print "is_cygwin=$is_cygwin\n";
	}
}
$sdir = "\\src\\";
$sdir2 = "/src/";
$sfiledir = "\\filesrc\\";
$sfiledir2 = "/filesrc/";
$presdir = "\\presrc\\";
$presdir2 = "/presrc/";
$presrc_main_javadir = "\\presrc\\main\\java\\";
$presrc_main_javadir2 = "/presrc/main/java/";
$compatdir = "\\compatpresrc\\";
$compatdir2 = "/compatpresrc/";
$testdir = "\\testpresrc\\";
$testdir2 = "/testpresrc/";
$slash = "\\";
$slash2 = "/";
$taget_mobile = '\\target\\mobile-rss-reader\\';
$taget_mobile2 = "/target/mobile-rss-reader/";
$taget_mobilesuite = "\\target\\mobilesuite\\";
$taget_mobilesuite2 = "/target/mobilesuite/";
$taget_oware = "\\target\\oware-midlet\\";
$taget_oware2 = "/target/oware-midlet/";
$taget_jmunit = "\\target\\jmunit-plus\\";
$taget_jmunit2 = "/target/jmunit-plus/";
$main_java = "\\main\\java\\";
$main_java2 = "/main/java/";
$test_java = "\\test\\java\\";
$test_java2 = "/test/java/";
$jlogmicro_proj = "\\JLogMicro\\jlogmicro";
$jlogmicro_proj2 = "/JLogMicro/jlogmicro";
$target_jlogmicro = "\\target\\jlogmicro\\";
$target_jlogmicro2 = "/target/jlogmicro/";
$taget_ebookplusme = "\\target\\ebookplusme\\";
$taget_ebookplusme2 = "/target/ebookplusme/";
%file_names = ();
%file_exists = ();
@wtk_projects = ();
push(@wtk_projects, '\\\\RSSReader\\\\mobile-rss-reader');
push(@wtk_projects, '\\\\RSSReadero\\\\mobile-rss-reader\\\\1.11.1');
push(@wtk_projects, '\\\\RSSReaderold\\\\mobile-rss-reader\\\\1.11.1');
push(@wtk_projects, '\\\\NewsReader\\\\mobilesuite');
push(@wtk_projects, '\\\\Oware\\\\oware-midlet');
push(@wtk_projects, '\\\\EBookPlusME\\\\ebookplusme');
@wtk_projects2 = ();
push(@wtk_projects2, '/RSSReader/mobile-rss-reader');
push(@wtk_projects2, '/RSSReadero/mobile-rss-reader/1.11.1');
push(@wtk_projects2, '/RSSReaderold/mobile-rss-reader');
push(@wtk_projects2, '/NewsReader/mobilesuite');
push(@wtk_projects2, '/Oware/oware-midlet');
push(@wtk_projects2, '/EBookPlusME/ebookplusme');

{
$test1 = <<'EOD'
 [wtkbuild] C:\opt\WTK2.5.1\apps\RSSReader\mobile-rss-reader\target\mobile-rss-reader\src\com\substanceofcode\rssreader\presentation\RssReaderMIDlet.java:618: cannot find symbol
 [wtkbuild] C:\opt\WTK2.5.1\apps\RSSReader\mobile-rss-reader\target\mobile-rss-reader\src\com\substanceofcode\rssreader\presentation\RssReaderMIDlet.java:725: cannot find symbol
EOD
};
{
$test2 = <<'EOD'
 [wtkbuild] C:\opt\WTK2.5.1\apps\RSSReader\mobile-rss-reader\target\mobile-rss-reader\src\com\substanceofcode\rssreader\presentation\AllNewsList.java:618: cannot find symbol
EOD
};
{
$test3 = <<'EOD'
     [copy] Copying 24 files to C:\opt\WTK2.5.1\apps\RSSReader\mobile-rss-reader\target\mobile-rss-reader\src
EOD
};
{
$test4 = <<'EOD'
     [copy] Copying 24 files to C:\opt\WTK2.5.1\apps\RSSReader\mobile-rss-reader\target\src
EOD
};
{
$test5 = <<'EOD'
[wtkbuild] C:\opt\WTK2.5.1\apps\RSSReader\mobile-rss-reader\target\mobile-rss-reader\src\com\substanceofcode\utils\SortUtil.java:66: illegal start of expression
EOD
};
{
$test6 = <<'EOD'
 [wtkbuild] C:\opt\WTK2.5.1\apps\RSSReader\mobile-rss-reader\target\mobile-rss-reader\src\com\substanceofcode\rssreader\businessentities\RssFeed1.java:49: cannot find symbol
 [wtkbuild] C:\opt\WTK2.5.1\apps\RSSReader\mobile-rss-reader\target\mobile-rss-reader\src\com\substanceofcode\rssreader\businessentities\RssFeed1.java:78: incompatible types
EOD
};
{
$test7 = <<'EOD'
 [wtkbuild] C:\opt\WTK2.5.1\apps\Reversi\mobilesuite\target\mobilesuite\Reversi\src\net\eiroca\j2me\reversi\ui\ReversiScreen.java:23: '.' expected
EOD
};
{
$test8 = <<'EOD'
 [wtkbuild] C:\opt\WTK2.5.1\apps\Oware\oware-midlet\target\oware-midlet\src\main\java\net\sf\yinlight\boardgame\oware\game\OwareTable.java:45: illegal forward reference
 [wtkbuild]   private final static int TABLE_STORE_SIZE = (NBR_ATTRIBUTES * OwareTable.NBR_COL * OwareTable.NBR_ROW) + (NBR_PLAYERS * 1) + (NBR_PLAYERS * 3) + 1;
EOD
};
{
$test9 = <<'EOD'
 [wtkbuild] C:\opt\WTK2.5.1\apps\Oware\oware-midlet\target\oware-midlet\src\main\java\net\sf\jlogmicro\util\presentation\RecStoreLoggerForm.java:203: ')' expected
 [wtkbuild] C:\opt\WTK2.5.1\apps\Oware\oware-midlet\target\oware-midlet\src\main\java\net\sf\jlogmicro\util\presentation\RecStoreLoggerForm.java:206: ')' expected
EOD
};
{
$test1b = <<'EOD'
 [wtkbuild] /opt/WTK2.5.1/apps/RSSReader/mobile-rss-reader/target/mobile-rss-reader/src/com/substanceofcode/rssreader/presentation/RssReaderMIDlet.java:618: cannot find symbol
 [wtkbuild] /opt/WTK2.5.1/apps/RSSReader/mobile-rss-reader/target/mobile-rss-reader/src/com/substanceofcode/rssreader/presentation/RssReaderMIDlet.java:725: cannot find symbol
EOD
};
{
$test1c = <<'EOD'
 [wtkbuild] /opt/WTK2.5.1/apps/EBookPlusME/ebookplusme/target/ebookplusme/src/main/java/cz/cacek/ebook/View.java:517: cannot find symbol
 [wtkbuild] symbol  : variable page
 [wtkbuild] location: class cz.cacek.ebook.View
 [wtkbuild] 		return (height-scrollHeight) * page.getPosition() / page.size;
 [wtkbuild] 		                                                    ^
 [wtkbuild] /opt/WTK2.5.1/apps/EBookPlusME/ebookplusme/target/ebookplusme/src/main/java/cz/cacek/ebook/EBookPlusMIDlet.java:111: cannot access cz.cacek.ebook.BackLight
 [wtkbuild] bad class file: /opt/WTK2.5.1/apps/EBookPlusME/ebookplusme/target/ebookplusme/src/main/java/cz/cacek/ebook/BackLight.java
 [wtkbuild] file does not contain class cz.cacek.ebook.BackLight
 [wtkbuild] Please remove or make sure it appears in the correct subdirectory of the classpath.
 [wtkbuild] 		BackLight tmpBL = BackLight.getInstance();
 [wtkbuild] 		^
 [wtkbuild] 17 errors
EOD
};
{
$test1d = <<'EOD'
Buildfile: /opt/WTK2.5.1/apps/RSSReadero/mobile-rss-reader/1.11.1/wtkbuild/wtk-build.xml
 [wtkbuild] /opt/WTK2.5.1/apps/RSSReadero/mobile-rss-reader/1.11.1/target/mobile-rss-reader/src/com/substanceofcode/rssreader/presentation/AllNewsList.java:233: cannot find symbol
 [wtkbuild] symbol  : variable m_itemFeedsNames
 [wtkbuild] location: class com.substanceofcode.rssreader.presentation.AllNewsList
 [wtkbuild] 		m_itemFeedsNames.removeAllElements();
 [wtkbuild] 		^
 [wtkbuild] 1 error
EOD
};
{
$test10 = <<'EOD'
 [wtkbuild] **************************************************************
 [wtkbuild] * Antenna 1.0.1 initialized for project "Oware"              *
 [wtkbuild] * Using Sun Wireless Toolkit 2.5 (CLDC-1.0; MIDP-2.0)        *
 [wtkbuild] **************************************************************
 [wtkbuild] Compiling 128 source files to C:\opt\WTK2.5.1\apps\Oware\oware-midlet\target\classes
 [wtkbuild] C:\opt\WTK2.5.1\apps\Oware\oware-midlet\target\oware-midlet\src\main\java\net\sf\yinlight\boardgame\oware\game\BoardGameScreen.java:947: computerTurn(net.eiroca.j2me.game.tpg.TwoPlayerGame,net.sf.yinlight.boardgame.oware.game.BoardGameMove) in net.sf.yinlight.boardgame.oware.game.BoardGameScreen cannot be applied to (net.sf.yinlight.boardgame.oware.game.BoardGameMove)
 [wtkbuild]       BoardGameMove computerMove = computerTurn(move);
 [wtkbuild]                                    ^
EOD
};
{
$test11 = <<'EOD'
    [javac] Compiling 6 source files to /opt/WTK2.5.1/apps/Oware/oware-midlet/target/j2se.classes
    [javac] /opt/WTK2.5.1/apps/Oware/oware-midlet/target/oware-midlet/src/main/java/net/sf/yinlight/j2se/sm/util/MakeSMFile.java:39: cannot find symbol
    [javac] symbol  : variable chiper
    [javac] location: class net.sf.yinlight.j2se.sm.util.MakeSMFile
    [javac]     data = chiper.encode(data, key);
    [javac]            ^
    [javac] /opt/WTK2.5.1/apps/Oware/oware-midlet/target/oware-midlet/src/main/java/net/sf/yinlight/j2se/sm/util/MakeSMFile.java:43: cannot find symbol
    [javac] symbol  : method FileReader(java.lang.String)
EOD
};
{
$test12 = <<'EOD'
 [wtkbuild] Compiling 43 source files to /opt/WTK2.5.1/apps/JMUnit/jmunit-plus/target/classes
 [wtkbuild] /opt/WTK2.5.1/apps/JMUnit/jmunit-plus/target/jmunit-plus/src/jmunit/framework/cldc11/AssertionMIDlet.java:35: class TestMIDlet is public, should be declared in a file named TestMIDlet.java
EOD
};
#$is_cygwin = 0; #undo
#$trace = 1;
#$trace = 2; #undo
#$trace = 3; #undo
#foreach $line (split("\n", $test12))
while (defined ($line = <>))
{
	if (($is_cygwin && ((($bpos = index ($line, "C:")) > 0) ||
	    (($bpos = index ($line, "D:")) > 0))) ||
	   (!$is_cygwin && (($bpos = index ($line, "/")) > 0))) {
		$file = substr ($line, $bpos);
		if ($trace) {
			print "\nline=$line\n";
			print "\nfile1=$file\n";
		};
		if ((($pos = index ($file, ":", $bpos)) < 0) && $is_cygwin) {
			print $line;
			if ($trace) {
				print "\nskipping file1=$file\n";
			};
			next;
		}
		if ($pos >= 0) {
			$file = substr ($file, 0, $pos);
			if ($trace) {
				print "\nmodified file1=$file\n";
			}
		}
		if ($trace) {
			print "file2=$file\n";
		}
		if (exists($file_exists{$file}) && !$file_exists{$file}) {
			if ($trace) {
				print "File does not exist file2=$file\n";
			}
			print $line;
			next;
		}
		if (exists($file_names{$file})) {
			$new_file = $file_names{$file};
			if ($trace) {
				print "File substituted new_file2=$new_file\n";
			}
		} else {
			if ($is_cygwin) {
				&modPaths($sdir, $sfiledir, $presdir, $presrc_main_javadir, $compatdir, $testdir,
						$slash, $taget_mobile, $taget_mobilesuite,
						$taget_oware, $taget_jmunit, $main_java, $test_java, $jlogmicro_proj,
						$target_jlogmicro,
						$taget_ebookplusme, @wtk_projects);
			} else {
				&modPaths($sdir2, $sfiledir2, $presdir2, $presrc_main_javadir2, $compatdir2, $testdir2, $slash2,
						$taget_mobile2, $taget_mobilesuite2, $taget_oware2,
						$taget_jmunit2,
						$main_java2, $test_java2, $jlogmicro_proj2, $target_jlogmicro2,
						$taget_ebookplusme2, @wtk_projects2);
			}
		}
		if (defined($new_file) && ($new_file ne "")) {
			$line = substr($line, 0, $bpos) . $new_file .
				substr($line, $bpos + length($file));
		}
	}
	print $line;
}

exit 0;

print "$test1 $test1b $test1c $test1d $test2 $test3 $test4 $test5 $test6 $test7 $test8 $test9 $test10 $test11 $test12 $is_cygwin\n";


sub subPaths {
	my($line, $findstr, $substr) = @_;
		if ($trace) {
			print "subline=$line\n";
		}
		my $pos = index($line, $findstr);
		if ($pos < 0) {
			return $line;
		}
		my $newline = substr($line, 0, $pos) . $substr .
			substr($line, $pos + length($findstr));
		if ($trace) {
			print "new subline=$newline\n";
		}
		return $newline;
}

sub modPaths {
	my($psdir, $psfiledir, $ppresdir, $ppresrc_main_javadir, $pcompatdir, $ptestdir, $pslash,
	$ptarget_mobile, $ptarget_mobilesuite, $ptarget_oware, $ptarget_jmunit,
	$pmain_java, $ptest_java, $pjlogmicro_proj, $ptarget_jlogmicro, $ptarget_ebookplusme,
	@pwtk_projects) = @_;
	
	$nfile = $file;
	$nfile = &subPaths($nfile, $ptarget_mobile, $pslash);
	if ($trace >= 3) {
		print "modPaths ptarget_mobile,nfile=$ptarget_mobile,$nfile\n";
	}
	$nfile = &subPaths($nfile, $ptarget_mobilesuite, $pslash);
	$nfile = &subPaths($nfile, $ptarget_oware, $pslash);
	$nfile = &subPaths($nfile, $ptarget_jmunit, $pslash);
	if ($trace >= 3) {
		print "modPaths ptarget_jmunit,nfile=$ptarget_jmunit,$nfile\n";
	}
	$nfile = &subPaths($nfile, $ptarget_ebookplusme, $pslash);
	if ($nfile ne $file) {
		if ($nfile =~ /jlogmicro/) {
			$nfile = &subPaths($nfile, $pmain_java, $pslash);
			$nfile = &subPaths($nfile, $ptest_java, $nfile);
			if ($trace) {
				print "removed main java nfile=$nfile\n";
			}
			foreach $str (@pwtk_projects) {
				if ($trace) {
					print "sub wtk str=$str\n";
				}
				$nfile =~ s#$str#$pjlogmicro_proj#;
				if ($trace) {
					print "sub replaced nfile=$nfile\n";
				}
			}
			$nfile =~ s#$ptarget_jlogmicro#$pslash#;
			if ($trace) {
				print "nfile=$nfile\n";
			}
		}
	}
	if ($trace) {
		print "nfile=$nfile\n";
	}
	local $cdir = $psdir;
	if ((($pos = index($nfile, $psdir)) > 0) || (defined($cdir = $psfiledir) &&
	   (($pos = index($nfile, $cdir)) > 0))) {
		$new_file = substr($nfile, 0, $pos) . $ppresdir .
			substr($nfile, $pos + length($cdir));
		if ($trace) {
			print "new_file=$new_file\n";
		}
		$new_presrc_main_file = substr($nfile, 0, $pos) . $ppresrc_main_javadir .
			substr($nfile, $pos + length($cdir));
		if ($trace) {
			print "new_presrc_main_file=$new_presrc_main_file\n";
		}
		my ($new_test_file);
		$new_compat_file = substr($nfile, 0, $pos) . $pcompatdir .
			substr($nfile, $pos + length($cdir));
		if ($trace) {
			print "new_compat_file=$new_compat_file\n";
		}
		$new_test_file = substr($nfile, 0, $pos) . $ptestdir .
			substr($nfile, $pos + length($psdir));
		if ($trace) {
			print "new_test_file=$new_test_file\n";
		}
		if (-e $new_file) {
			$file_names{$file} = $new_file;
			$file_names{$file} =~ s#\\#/#g;
			$file_exists{$file} = 1;
			if ($trace) {
				print "new_file exists=$new_file\n";
			}
		} elsif (-e $new_presrc_main_file) {
			$file_names{$file} = $new_presrc_main_file;
			$file_names{$file} =~ s#\\#/#g;
			$file_exists{$file} = 1;
			$new_file = $new_presrc_main_file;
			if ($trace) {
				print "new_presrc_main_file exists=$new_presrc_main_file\n";
			}
		} elsif (-e $new_compat_file) {
			$file_names{$file} = $new_compat_file;
			$file_names{$file} =~ s#\\#/#g;
			$file_exists{$file} = 1;
			$new_file = $new_compat_file;
			if ($trace) {
				print "new_compat_file exists=$new_compat_file\n";
			}
		} elsif (-e $new_test_file) {
			$file_names{$file} = $new_test_file;
			$file_names{$file} =~ s#\\#/#g;
			$file_exists{$file} = 1;
			$new_file = $new_test_file;
			if ($trace) {
				print "new_test_file exists=$new_test_file\n";
			}
		} elsif (-e $nfile) {
			$new_file = $nfile;
			$file_names{$file} = $new_file;
			$file_names{$file} =~ s#\\#/#g;
			$file_exists{$file} = 1;
			if ($trace) {
				print "new new_file=$new_file\n";
			}
		} else {
			if ($trace) {
				print "File does not exist new_file=$new_file\n";
			}
			$file_exists{$file} = 0;
			print $line;
			return 0;
		}
	} else {
		print $line;
		return 0;
	}
	return 1;
}
